// pages/invoice/invoice.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoice_list: [
      { name: "电子普通发票"},
      { name: "普通发票" }
    ],
    tip_list: [],
    formData:{},
    start_date: '',
    date: '',
    disabled: true,
  },
  bindDateChange: function (e) {
    if (e.detail.value) {
      this.start_date = true
      this.setData({
        start_date: e.detail.value
      })
    }
    
    if (
      this.license_name &&
      this.id_number &&
      this.address &&
      this.phone &&
      this.bank &&
      this.bank_account &&
      this.start_date &&
      this.date
    ) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  choose_date: function (e) {
    if (e.detail.value) {
      this.date = true
      this.setData({
        date: e.detail.value
      })
    }
    
    if (
      this.license_name &&
      this.id_number &&
      this.address &&
      this.phone &&
      this.bank &&
      this.bank_account &&
      this.start_date &&
      this.date
    ) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  check: function (e) {
    var index = e.currentTarget.id
    var invoice_list = this.data.invoice_list
    for (let i in invoice_list){
      invoice_list[i].checked = false
    }
    invoice_list[index].checked = true
    this.setData({
      in_active: index,
      invoice_list: invoice_list
    })
  },
  disabled: function (e) {
    if (e.currentTarget.dataset.name == "license_name" && e.detail.value != '') {
      this.license_name = true
    }
    if (e.currentTarget.dataset.name == "license_name" && e.detail.value == '') {
      this.license_name = false
    }
    if (e.currentTarget.dataset.name == "id_number" && e.detail.value != '') {
      this.id_number = true
    }
    if (e.currentTarget.dataset.name == "id_number" && e.detail.value == '') {
      this.id_number = false
    }
    if (e.currentTarget.dataset.name == "address" && e.detail.value != '') {
      this.address = true
    }
    if (e.currentTarget.dataset.name == "address" && e.detail.value == '') {
      this.address = false
    }
    if (e.currentTarget.dataset.name == "phone" && e.detail.value != '') {
      this.phone = true
    }
    if (e.currentTarget.dataset.name == "phone" && e.detail.value == '') {
      this.phone = false
    }
    if (e.currentTarget.dataset.name == "bank" && e.detail.value != '') {
      this.bank = true
    }
    if (e.currentTarget.dataset.name == "bank" && e.detail.value == '') {
      this.bank = false
    }
    if (e.currentTarget.dataset.name == "bank_account" && e.detail.value != '') {
      this.bank_account = true
    }
    if (e.currentTarget.dataset.name == "bank_account" && e.detail.value == '') {
      this.bank_account = false
    }
    if (
      this.license_name &&
      this.id_number &&
      this.address &&
      this.phone &&
      this.bank &&
      this.bank_account &&
      this.start_date &&
      this.date
    ) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  commit: function (e) {
    var license_name = e.detail.value.license_name,
      id_number = e.detail.value.id_number,
      address = e.detail.value.address,
      phone = e.detail.value.phone,
      bank = e.detail.value.bank,
      bank_account = e.detail.value.bank_account,
      time = this.data.date,
      start_time = this.data.start_date,
      email = e.detail.value.email,
      invoice_list = this.data.invoice_list,
      license_type = -1
    for (let i in invoice_list){
      if (invoice_list[i].checked){
        license_type = i
      }
    }
    app.ajax({
      url: app.host + 'mall/User/invoice',
      data: {
        license_type: license_type,
        license_name: license_name,
        id_number: id_number,
        address: address,
        phone: phone,
        bank: bank,
        bank_account: bank_account,
        time: time,
        start_time: start_time,
        email: email
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 1000
        })
        setTimeout(function(){
          wx.navigateBack()
        },1000)
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中'
    })
    app.ajax({
      url: app.host + 'mall/user/invoice',
      success:function(res){
        wx.hideLoading()
        var license_type = res.data.data.license_type
        console.log(license_type)
        var invoice_list = that.data.invoice_list
        for (let i in invoice_list){
          invoice_list[i].checked = false
        }
        if (license_type){
          invoice_list[license_type].checked = true
        }else{
          invoice_list[0].checked = true
        }
        that.setData({
          formData:res.data.data,
          invoice_list: invoice_list,
          tip_list:res.data.data.invoice,
          disabled:false,
          start_date: res.data.data.start_time,
          date: res.data.data.time
        })
          that.license_name = true
          that.id_number = true
          that.address = true
          that.phone = true
          that.bank = true
          that.bank_account = true
          that.start_date = true
          that.date = true
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      // app.ajax({
      //     url: app.host + 'mall/User/invoice',
          
      //     success: function (res) {
      //        console.log(res);
      //     }
      // });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})